﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Sublimation
{
    public class EnergyBall : GameObject
    {
        protected GameObject target;
        public GameObject Target
        {
            get { return target; }
        }
        protected float energyTransported;
        public EnergyBall(WorldProperties worldproperties, GameObject target, Vector2 position, float energyTransported)
            : base(worldproperties)
        {
            this.target = target;

            this.energyTransported = energyTransported;
            DrawOrder = 70;
            Scale = 0.7f;
            MaxSpeed = 500;
            CollisionRadius = 10;
            MaxTurnRadiansPerSec = -1f;
            
            SetOriginToCentre();
            this.Position = position;

            velocity = (Target.Position - Position);
            Rotation = (float)Math.Atan2(velocity.Y, velocity.X);
            velocity.Normalize();
            Velocity *= MaxSpeed;
            
        }

        public override void CheckCollisions(Microsoft.Xna.Framework.GameTime gameTime)
        {
            base.CheckCollisions(gameTime);
            if (CollidesWith(target))
            {
                ((Tower)target).Energy.Amount += energyTransported;
                Kill();
            }
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            Rotation = 2*MathHelper.Pi*(float)worldProperties.Random.NextDouble();
        }

        protected override void LoadContent()
        {
            base.LoadContent();
            Sprite = Game.Content.Load<Texture2D>("Decals/energy");
        }
        
        protected override void SetupBehaviours()
        {
            base.SetupBehaviours();
            this.behaviours.Add(new Behaviours.EnergyBallBehaviour(worldProperties, this, 1f));
            this.behaviours.Add(new Behaviours.UnitInertiaBehaviour(worldProperties, this, 1f));
        }
    }
}
